package com.movie.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.movie.model.auto.*;
import com.movie.service.IFilmService;
import com.movie.service.IRoleService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 电影表 前端控制器
 * </p>
 *
 * @author 达瓦里氏
 * @since 2021-12-16
 */
@RestController
@RequestMapping("/film")
@CrossOrigin
public class FilmController {

    @Resource
    private IFilmService iFilmService;
    /*查询电影*/
    @GetMapping("/getFilm")
    public List<Film> getFilm(){ return iFilmService.list(); }

    /*通过id查询电影*/
    @GetMapping("/getFilmById/{id}")
    public Film getFilmById(@PathVariable("id") int id){
        return iFilmService.getById(id);
    }

    /*修改电影*/
    @PostMapping("/updateFilm")
    public void updateFilm(@RequestBody Film film){
        iFilmService.updateById(film);
    }

    /*添加电影*/
    @PostMapping("/setFilm")
    public void setFilm(@RequestBody Film film){
        iFilmService.save(film);
    }

    /*删除电影*/
    @PostMapping("/deletedById/{id}")
    public boolean deleted(@PathVariable("id") int id){
        return iFilmService.removeById(id);
    }


    /*批量删除电影*/
    @PostMapping("/deletedAllById")
    public boolean deletedAllById(@RequestBody int[] delarr){
        for (int i : delarr) {
            System.out.println(i);
            iFilmService.removeById(i);
        }
        return true;
    }

    /*模糊查询电影数据*/
    @PostMapping("/selectFilm/{input}")
    public List<Map<String, Object>> selectUser(@PathVariable("input") String value){
        System.out.println(value);
        QueryWrapper<Film> queryWrapper = new QueryWrapper<>();
        /*核心模糊算法*/
        queryWrapper.like("name",value).or()
                .like("director",value).or()
                .like("producer",value).or()
                .like("country",value).or()
                .like("actors",value).or()
                .like("language",value).or()
                .like("introduce",value).or()
                .like("length",value).or();
        List<Map<String, Object>> maps = iFilmService.listMaps(queryWrapper);
        for (Map<String, Object> map : maps) {
            System.out.println(map);
        }
        return maps;
    }


    /*图片上传*/
    @PostMapping("/elupload")
    public String elUpload(MultipartFile file){
        String fileName = file.getOriginalFilename();
        System.out.println("进来了");
        File dest = new File("D:\\project\\movies\\movie-vue\\static\\img\\" + fileName);
        try {
            file.transferTo(dest);
            return "上传成功";
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "上传失败！";
    }

    /*查询热门电影*/
    @GetMapping("/selectFilmByHot")
    public List<HotFilm> SelectFilmByHot(){
        return iFilmService.SelectFilmByHot();
    }


    /*查询最新电影*/
    @GetMapping("/selectFilmByNews")
    public List<NewsFilm> SelectFilmByNews(){
        return iFilmService.SelectFilmByNews();
    }

    /*根据类别查询电影*/
    @PostMapping("/getClassFilm/{id}")
    public List<Map<String, Object>> getClassFilm(@PathVariable("id") int id){
        System.out.println(id);
        QueryWrapper<Film> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category",id);
        List<Map<String, Object>> maps = iFilmService.listMaps(queryWrapper);
        return maps;
    }

    /*前台显示的电影*/
    @GetMapping("/selectFilmById/{id}")
    public FilmAccount selectFilmById(@PathVariable("id")int id){
        return iFilmService.selectFilmById(id);
    }

    /*查询电影*/
    @GetMapping("/getAllFilm")
    public List<AllFilm> getAllFilm(){
        return iFilmService.SelectAllFilm();
    }


}
